package pvt.test.democountryapdater;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnQueryTextListener {
	private ListView listView;
	ArrayList<Country> countries;
	CountryAdapter adapter;
	ActionMode actionMode;

	Callback callback = new Callback() {

		@Override
		public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onDestroyActionMode(ActionMode arg0) {
			// TODO Auto-generated method stub
			// adapter.notifyDataSetChanged();

		}

		@Override
		public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
			// TODO Auto-generated method stub
			getMenuInflater().inflate(R.menu.main, menu);
			return true;
		}

		@Override
		public boolean onActionItemClicked(ActionMode arg0, MenuItem items) {
			// TODO Auto-generated method stub
			switch (items.getItemId()) {
			case R.id.del:

				for (int i = 0; i < countries.size(); i++) {
					Country country = countries.get(i);
					if (country.isChecked() == true) {
						countries.remove(i);
						i--;
					}

				}
				adapter.notifyDataSetChanged();

				break;

			default:
				break;
			}
			return true;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.listView1);
		Resources resources = getResources();
		countries = new ArrayList<Country>();
		TypedArray arrayFlag = resources.obtainTypedArray(R.array.flag);
		TypedArray arrayNameEn = resources.obtainTypedArray(R.array.nameEn);
		TypedArray arrayNameVi = resources.obtainTypedArray(R.array.nameVI);

		for (int i = 0; i < arrayFlag.length(); i++) {
			Drawable drawable = arrayFlag.getDrawable(i);
			String nameEn = arrayNameEn.getString(i);
			String nameVi = arrayNameVi.getString(i);
			Country country = new Country(drawable, nameEn, nameVi);
			countries.add(country);
		}

		adapter = new CountryAdapter(this, R.layout.layout_cell, countries);
		listView.setAdapter(adapter);

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int pos = contextMenuInfo.position;

		switch (item.getItemId()) {
		case R.id.del:

			Toast.makeText(getApplicationContext(),
					"Delete :" + contextMenuInfo.position, Toast.LENGTH_LONG)
					.show();
			countries.remove(pos);
			adapter.notifyDataSetChanged();

			break;

		case R.id.edit:
			Toast.makeText(getApplicationContext(),
					"Edit" + contextMenuInfo.position, Toast.LENGTH_LONG)
					.show();

			break;
		default:
			break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_menu, menu);
		MenuItem item = menu.findItem(R.id.menu_search);
		SearchView searchView = (SearchView) item.getActionView();
		searchView.setOnQueryTextListener(this);
		
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.main, menu);
		int pos = ((AdapterContextMenuInfo) menuInfo).position;
		if (pos == 0) {
			MenuItem item = menu.getItem(0);
			item.setVisible(false);

		}
		super.onCreateContextMenu(menu, v, menuInfo);

	}

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		Log.i("PVT", newText);
		adapter.getFilter().filter(newText);
		return true;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		// TODO Auto-generated method stub
		return false;
	}

}
